package com.cnpc.rightslogan.feign;

import com.cnpc.rightslogan.entity.Organization;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Collections;

@FeignClient(value = "rightslogan-organization", fallback = OrganizationClientServiceFallback.class)
public interface IOrganizationClientService {

    @RequestMapping(value = "/organization/getOrganization/{orgId}", method = RequestMethod.GET)
    Organization getOrganization(@PathVariable("orgId") int orgId);

    @RequestMapping(value = "/api/organization/list2", method = RequestMethod.POST)
    PageInfo<Organization> getOrganizationList(@RequestBody String json);

}

/**
 * 回退类OrganizationClientServiceFallback需实现IOrganizationClientService接口
 * OrganizationClientServiceFallback也可以是public class，没有区别
 */
@Component
@Slf4j
class OrganizationClientServiceFallback implements IOrganizationClientService {

    @Override
    public Organization getOrganization(int orgId) {
        log.error("断路器getOrganization显灵了");
        Organization organization = new Organization();
        organization.setOrgName("报错了");
        return organization;
    }

    @Override
    public PageInfo<Organization> getOrganizationList(String json) {
        log.error("断路器getOrganizationList显灵了");
        Organization organization = new Organization();
        organization.setOrgName("报错了");
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(Collections.singletonList(new ArrayList().add(organization)));
        return pageInfo;
    }
}